package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljStorageBoxClean;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.TroubleshootingService;
import com.guofen.dao.ShljTroubleshootingMapper;
import com.guofen.pojo.ShljTroubleshooting;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TroubleshootingServiceImpl implements TroubleshootingService{
    @Autowired
    private ShljTroubleshootingMapper shljTroubleshootingMapper;

    @Override
    public boolean add(String personInCharge,
                       String systemName,
                       String equipmentName,
                       String stage,
                       String recordingPerson,
                       String timeOfFailure,
                       String faultDescription,
                       String faultReasion,
                       String faultRepairMethod,
                       String repairStartTime,
                       String repairEndTime,
                       String repairPerson,
                       String methodProposer){
        String date = DateAndTimeUtil.getDate();

        ShljTroubleshooting shljTroubleshooting = new ShljTroubleshooting();
        shljTroubleshooting.setDate(date);
        shljTroubleshooting.setPersonInCharge(personInCharge);
        shljTroubleshooting.setSystemName(systemName);
        shljTroubleshooting.setEquipmentName(equipmentName);
        shljTroubleshooting.setStage(stage);
        shljTroubleshooting.setRecordingPerson(recordingPerson);
        shljTroubleshooting.setTimeOfFailure(timeOfFailure);
        shljTroubleshooting.setFaultDescription(faultDescription);
        shljTroubleshooting.setFaultReasion(faultReasion);
        shljTroubleshooting.setFaultRepairMethod(faultRepairMethod);
        shljTroubleshooting.setRepairStartTime(repairStartTime);
        shljTroubleshooting.setRepairEndTime(repairEndTime);
        shljTroubleshooting.setRepairPerson(repairPerson);
        shljTroubleshooting.setMethodProposer(methodProposer);

        int result = shljTroubleshootingMapper.insert(shljTroubleshooting);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public ShljTroubleshooting getlast(){
        return shljTroubleshootingMapper.getLast();
    }

    @Override
    public List<ShljTroubleshooting> getList() {
        return shljTroubleshootingMapper.getList();
    }

    @Override
    public boolean addFrontPart(String personInCharge, String systemName, String equipmentName, String stage, String recordingPerson, String timeOfFailure, String faultDescription) {
        String date = DateAndTimeUtil.getDate();

        ShljTroubleshooting shljTroubleshooting = new ShljTroubleshooting();
        shljTroubleshooting.setDate(date);
        shljTroubleshooting.setPersonInCharge(personInCharge);
        shljTroubleshooting.setSystemName(systemName);
        shljTroubleshooting.setEquipmentName(equipmentName);
        shljTroubleshooting.setStage(stage);
        shljTroubleshooting.setRecordingPerson(recordingPerson);
        shljTroubleshooting.setTimeOfFailure(timeOfFailure);
        shljTroubleshooting.setFaultDescription(faultDescription);

        int result = shljTroubleshootingMapper.insertSelective(shljTroubleshooting);
        if (result == 1)
            return true;
        return false;
    }

    @Override
    public boolean addAfterPart(Integer id, String faultReasion, String faultRepairMethod, String repairStartTime, String repairEndTime, String repairPerson, String methodProposer) {
        ShljTroubleshooting shljTroubleshooting = new ShljTroubleshooting();
        shljTroubleshooting.setId(id);
        shljTroubleshooting.setFaultReasion(faultReasion);
        shljTroubleshooting.setFaultRepairMethod(faultRepairMethod);
        shljTroubleshooting.setRepairStartTime(repairStartTime);
        shljTroubleshooting.setRepairEndTime(repairEndTime);
        shljTroubleshooting.setRepairPerson(repairPerson);
        shljTroubleshooting.setMethodProposer(methodProposer);

        int result = shljTroubleshootingMapper.updateByPrimaryKeySelective(shljTroubleshooting);
        if (result == 1)
            return true;
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
        int result = shljTroubleshootingMapper.deleteByPrimaryKey(id);
        if (result == 1)
            return true;
        return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljTroubleshooting> list = shljTroubleshootingMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljTroubleshooting> list = shljTroubleshootingMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljTroubleshootingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljTroubleshooting record) {
        return shljTroubleshootingMapper.updateByPrimaryKeySelective(record);
    }
}